<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Assessment extends CI_Controller {

        function __construct() {

                // Need to run CI_Controller constructor
                parent::__construct();

                // Load the user model
                // This is needed for checking user is logged in and has permission to access the page
                $this->load->model('user');
				$this->load->model('assessments');

        }
		
		public function index() {

		// Check if the user is logged in and has permission to view page
            if ( ! $this->user->is_logged()  ) { redirect('welcome/login'); }
            if ( $this->user->is_administrator() || $this->user->is_tutor() || $this->user->is_lecturer() ){
            
            // LECTURERS, TUTORS & ADMINS
            // Page title to send to view
            $data['page_title'] = "Assessment";
            // Page content 
            $data['main_content'] = "assessment/assessment_grid";
            // Title Icon
            $data['title_icon'] = "assessment";
            
           // Template Settings
            $data['sidebar_modifier'] = 'Hide Both';
			//$data['left_sidebar_icon'] = '';
			//$data['left_sidebar_title'] = '';
			//$data['content_icon'] = '';
			$data['content_title'] = 'Assessment Items';
			//$data['right_sidebar_icon'] = '';
			$data['right_sidebar_title'] = 'Actions';
	
			//$data['left_sidebar'] = 'my_group';
			//$data['right_sidebar'] = 'my_account';
            
                //Remember userid for later use
            	$userid = $this->session->userdata('user_id');
            
			
			$future_assessments = $this->assessments->get_future_items();
			$data['future_items'] = $future_assessments;
			
			$past_assessments = $this->assessments->get_past_items();
			$data['past_items'] = $past_assessments;
            
            
            }else{
            // STUDENTS
            
            	//Remember userid for later use
            	$userid = $this->session->userdata('user_id');
            
            // Page title to send to view
            $data['page_title'] = "Assessment";
            // Page content 
            $data['main_content'] = "assessment/assessment_sdue";
        	
            // Title Icon
            $data['title_icon'] = "assessment";
            
           // Group Name
           	$groupname = $this->assessments->get_group_name($userid);
           	$data['group_name'] = $groupname;
           	
           	if(!$groupname){$groupname = "My Group";}
            
           // Template Settings
			$data['left_sidebar_icon'] = 'groups';
			$data['left_sidebar_title'] = $groupname;
			$data['content_icon'] = 'itemsdue';
			$data['content_title'] = 'Assessment Items Due';
			$data['right_sidebar_icon'] = 'grades';
			$data['right_sidebar_title'] = 'Grades';
	
			$data['right_sidebar'] = 'assessment_grades';
			$data['left_sidebar'] = 'assessment_group';
		
			$assessments = $this->assessments->get_assessment_items($userid);
			$data['assessment_items'] = $assessments;
			
			date_default_timezone_set('Australia/Brisbane');
			$week_no = date("W");
			$peer_review_submitted = $this->assessments->_check_peer_review($userid,$week_no);
			$data['has_peer_review'] = $peer_review_submitted;
			$data['week_no'] = $week_no;
			
            
            }
            

            // Load the welcome page for the admin section
            $this->load->view('template', $data);
        }
        
       public function delete() {

			// Check if the user is logged in and has permission to view page
            if ( ! $this->user->is_logged()  ) { redirect('welcome/login'); }
            if ( !$this->user->is_administrator() ){ redirect('welcome/login'); }
            
			else{
            
            // Page title to send to view
            if($this->uri->segment(3) != null){
	            $itemid = $this->uri->segment(3);
	            //if(is_int($itemid)){
	            $this->assessments->delete_assessment($itemid);
	            //}
	            if(isset($_GET['ap'])){
            	redirect('assessment');}
            	else{
            	redirect('assessment/delete');}
            	
           
            
            }else{
            // Page content 
            $data['page_title'] = "Delete Assessment Items";
            $data['main_content'] = "assessment/assessment_delete";

			$future_assessments = $this->assessments->get_future_items();
			$data['future_items'] = $future_assessments;
			
			$past_assessments = $this->assessments->get_past_items();
			$data['past_items'] = $past_assessments;

            // Load the welcome page for the admin section
            $this->load->view('template', $data);
            }
            }
        }
        
        public function modify() {
			
			 if(isset($_POST['title'])){
            	$itemid = stripslashes($_POST['itemid']);
            	$data['assessment_title'] = stripslashes($_POST['title']);
            	$data['due_date'] = $this->assessments->_format_due_date($_POST['date'],$_POST['time']);
            	$data['weight'] = stripslashes($_POST['weight']);
            	$data['description'] = stripslashes($_POST['desc']);
            	$this->assessments->change_assessment($itemid,$data);
            	redirect('assessment/');
            
            }else{
			// Check if the user is logged in and has permission to view page
            if ( ! $this->user->is_logged()  ) { redirect('welcome/login'); }
			if ( ! $this->user->is_administrator() && ! $this->user->is_tutor()) { redirect('assessment/view/'.$this->uri->segment(3)); }
            // Page title to send to view
                $data['page_title'] = "Assessment";
            	// Title Icon
            	$data['title_icon'] = "assessment";
            	
            	           // Template Settings
            $data['sidebar_modifier'] = 'Hide Both';
			//$data['left_sidebar_icon'] = '';
			//$data['left_sidebar_title'] = '';
			//$data['content_icon'] = '';
			$data['content_title'] = 'Assessment Item Modification';
			//$data['right_sidebar_icon'] = '';
			//$data['right_sidebar_title'] = '';
			
            // Page content
            $data['main_content'] = "assessment/assessment_modify";
	
            // Data for the page
            $item_id = $this->uri->segment(3);
            $data['item_details'] = $this->assessments->get_assessment_details($item_id);
            $data['total_weight'] = $this->assessments->get_total_weight();

            // Load the welcome page for the admin section
            $this->load->view('template', $data);
            }
        }
        
       public function view() {

			// Check if the user is logged in and has permission to view page
            if ( ! $this->user->is_logged()  ) { redirect('welcome/login'); }
            // Page content 
            $item_id = $this->uri->segment(3);
            $data['item_details'] = $this->assessments->get_assessment_details($item_id);
            
            if ( $this->user->is_administrator() || $this->user->is_tutor() || $this->user->is_lecturer() ){
            	//What to show for anyone whom is greater than a student
            	
            	// Page title to send to view
            	
            	// Page title to send to view
            	$data['page_title'] = "Assessment";
            	$data['main_content'] = "assessment/assessment_view-mark";
            	// Title Icon
            	$data['title_icon'] = "assessment";
            	
            	           // Template Settings
            $data['sidebar_modifier'] = 'Hide Both';
			//$data['left_sidebar_icon'] = '';
			//$data['left_sidebar_title'] = '';
			//$data['content_icon'] = '';
			$data['content_title'] = 'Assessment Viewer and Marker';
			//$data['right_sidebar_icon'] = '';
			//$data['right_sidebar_title'] = '';

            	
            	
            	$data['submissions'] = $this->assessments->get_submissions($item_id);
            }else{
            	//What to show to a student
            	
            	//Remember userid for later use
            	$userid = $this->session->userdata('user_id');
            	
            	// Page title to send to view
            	$data['page_title'] = "Assessment";
            	$data['main_content'] = "assessment/assessment_view";
            	// Title Icon
            	$data['title_icon'] = "assessment";
            	
            	           // Template Settings
            $data['sidebar_modifier'] = 'Hide Left';
			//$data['left_sidebar_icon'] = '';
			//$data['left_sidebar_title'] = '';
			//$data['content_icon'] = '';
			$data['content_title'] = 'Assessment Viewer';
			//$data['right_sidebar_icon'] = '';
			$data['right_sidebar_title'] = 'Submission';
	
			//$data['left_sidebar'] = 'my_group';
			$data['right_sidebar'] = 'assessment_vsub';
            	
            	$data['submitted'] = $this->assessments->is_assessment_submitted($item_id,$userid);
            	
            	
            }
			

            // Load the welcome page for the admin section
            $this->load->view('template', $data);
        }

      public function peerreview() {

		// Check if the user is logged in and has permission to view page
            if ( ! $this->user->is_logged()  ) { redirect('welcome/login'); }
            
            if ( $this->user->is_administrator() || $this->user->is_tutor() || $this->user->is_lecturer() ){
	            redirect('assessment');
            }else{

                  // Load models
                  $this->load->model('groups');

                  //Remember userid for later use
                  $userid = $this->session->userdata('user_id');
                  $group_id = $this->groups->student_in_group($userid);
                  $week_no = '1';

                  // If they have submitted a peer review
                  if ( $this->input->post('receiver_id') ) {
                        $data = $this->input->post();
                        $data['sender_id'] = $userid;
                        $data['group_id'] = $group_id;
                        $data['week_no'] = date("W");

                        // Check if they have already submitted a peer review
                        if ( $this->assessments->check_peer_review_exists($data['sender_id'], $data['receiver_id'], $data['assessment_id'], $data['week_no'])) {
                              $this->session->set_flashdata('errors', 'You have already submitted a peer review for this student this week. (Make sure you selected the correct assessment item)' );
                              redirect('assessment/peerreview'); 
                        }

                        // Add peer review
                        if ( $this->assessments->add_peer_review($data) ) {
                              $this->session->set_flashdata('notices', 'Peer review submitted.' );
                        } else {
                              $this->session->set_flashdata('errors', 'An error occured. Please try again.' );
                        }
                        redirect('assessment/peerreview'); 
                  }


                  //What to show to a student

            	// Page title to send to view
            	$data['page_title'] = "Assessment";
            	$data['main_content'] = "assessment/assessment_peer_review";
            	// Title Icon
            	$data['title_icon'] = "assessment";
            	
            	// Template Settings
                  $data['sidebar_modifier'] = 'Hide Both';
			$data['content_title'] = 'Peer Review Submission';

                   // DATA
                  // Get group members
                  $group_id = $this->groups->student_in_group($userid);
                  $data['group_members'] = $this->groups->group_members($group_id, true);
                  // Assessment list
                  $data['assessment_list'] = $this->assessments->get_all_assessment_items();
                  $data['user_id'] = $userid;
            	
            }
			

            // Load the welcome page for the admin section
            $this->load->view('template', $data);
        }


        
       public function create() {

			// Check if the user is logged in and has permission to view page
            if ( ! $this->user->is_logged()  ) { redirect('welcome/login'); }
			if ( ! $this->user->is_administrator() && ! $this->user->is_tutor()) { redirect('assessment'); }
            // Page title to send to view
            
            if(isset($_POST['title'])){
            
            	$data['assessment_title'] = stripslashes($_POST['title']);
            	$data['due_date'] = $this->assessments->_format_due_date($_POST['date'],$_POST['time']);
            	$data['weight'] = $_POST['weight'];
            	$data['description'] = $_POST['desc'];
            	$this->assessments->create_assessment($data);
            	redirect('assessment/');
            
            }else{
                $data['page_title'] = "Assessment";
            	// Title Icon
            	$data['title_icon'] = "assessment";
            	
            	           // Template Settings
            $data['sidebar_modifier'] = 'Hide Both';
			//$data['left_sidebar_icon'] = '';
			//$data['left_sidebar_title'] = '';
			//$data['content_icon'] = '';
			$data['content_title'] = 'Assessment Item Creation';
			//$data['right_sidebar_icon'] = '';
			//$data['right_sidebar_title'] = '';
            
            
            // Page content 
            $data['main_content'] = "assessment/assessment_create";
            
            // Data for the page
            $data['total_weight'] = $this->assessments->get_total_weight();
			

            // Load the welcome page for the admin section
            $this->load->view('template', $data);
			}
        }

		public function submit() {

			// Check if the user is logged in and has permission to view page
            if ( ! $this->user->is_logged()  ) { redirect('welcome/login'); }
			if ( $this->user->is_administrator() || $this->user->is_tutor() || $this->user->is_lecturer()) { redirect('assessment'); 
			}else{
			//Must be a student to submit an assessment
				if (isset($_POST['itemid'])){
					$item = $_POST['itemid'];
					$userid = $this->session->userdata('user_id');
						$this->assessments->submit_assessment($item,$userid);
						redirect('assessment/view/'.$item);
						
					
				}
			}
        }
        
        public function mark() {

			// Check if the user is logged in and has permission to view page
            if ( ! $this->user->is_logged()  ) { redirect('welcome/login'); }
			if ( $this->user->is_administrator() || $this->user->is_tutor() || $this->user->is_lecturer()) {
			//Cannot be a student to mark assessment
				if (isset($_POST['subid']) && isset($_POST['gradid']) && isset($_POST['itmid'])){
					$item = $_POST['subid'];
					$grade = (int)$_POST['gradid'];
					$aitem = $_POST['itmid'];
						if($grade <= 100 && $grade >= 0){
						$this->assessments->set_assessment_result($item,$grade);
						}
						redirect('assessment/view/'.$aitem);
						
					}else{
						redirect('assessment');
					}
			}else{ redirect('assessment'); 
			}
        }

				
}
?>